set more off
cls clear screen

global map `"your directory here"'



use "${map}/fy1975.dta"



rename fyear year
rename *, lower
rename gvkey gvkey1
destring gvkey1, gen(gvkey)



xtset gvkey year



*drop financial firms, regulated firms 
destring sic,replace

drop if sic>=4900&sic<=4999
drop if sic>=6000&sic<=6999
drop if sic>=9000

drop if year<1974


drop if missing(sale)
drop if missing(at)
drop if at<0
drop if sale<0
drop if ppegt<5


save "${map}/fy1975.dta", replace


*Taylor_Peters_2022 variables
use "${map}/fy1975.dta"
merge m:1 gvkey year using "${map}/Raw data/Taylor_Peters_2022.dta",/* 
*/keepusing(gvkey1 K_int_Know K_int_Org K_int_offBS K_int q_tot) generate(_merge_q)
drop if _merge_q==2
drop _merge_q



*the TFP data from 2014 MS
merge m:1 gvkey year using "${map}/Raw data/MS 2014 TFP updated.dta", keepusing(tfp)
drop if _merge==2 
drop _merge




////////////generate regression variables

*physical capital
gen k_phy=ppegt

*intangible capital
rename K_int k_int

*total capital
gen k_tot=k_phy+k_int

*physical investment
gen inv_phy=capx/l.k_tot

*intangible investment
gen xrd1=xrd
replace xrd1=0 if xrd==.
gen intan_inv=xrd1+0.3*xsga
gen inv_int=intan_inv/l.k_tot

*R&D investment
gen RD=xrd1/l.k_tot

*total investment
gen inv_tot=inv_phy+inv_int

*market value
gen v=(dltt+dlc+prcc_c*csho-act)

*standard TobinsQ
gen TQ=v/ppegt

*traditional investment
gen inv=capx/l.ppegt

*standard cash flow
gen CF=(ib+dp)/l.ppegt

*total cash flow
gen CF_tot=(ib+dp+intan_inv*0.7)/(l.k_phy+l.k_int)




/*******************KZ, WW financial constraints variables*********************/

/////////financial constraints by KZ index 
gen kz=-1.001909*CF + 0.2826389 *TQ + 3.139193 *(dltt+dlc+np)/at -39.3678* (dvc/l.ppegt) -1.314759 *ch/l.ppegt


bys year: egen kz_median=median(kz)
gen fc_kz=.
replace fc_kz=1 if kz>=kz_median & kz!=.
replace fc_kz=0 if kz<kz_median & kz!=.
drop kz_median 

save "${map}/fy1975.dta", replace 

//////////financial constraints by WW index 
*industry sales growth
collapse (sum) sale, by(sic3 year)

rename sale tot_sale 
xtset sic3 year
gen isg=(tot_sale/l.tot_sale-1 )
save "${map}/Temp data/industry sales growth.dta", replace

use "${map}/fy1975.dta"
merge m:1 sic3 year using "${map}/Temp data/industry sales growth.dta", keepusing(isg)
drop if _merge==2
drop _merge 

gen dividend=0
replace dividend=1 if dvp+dvc>0 
xtset gvkey year
gen sg=(sale/l.sale-1)
gen ww=-0.091*((ib+dp)/at)-0.062*dividend+0.021*(dltt/at)-0.044*ln(at)+0.102*isg-0.035*sg

 
bys year: egen ww_median=median(ww)
gen fc_ww=.
replace fc_ww=1 if ww>=ww_median & ww!=.
replace fc_w=0 if ww<ww_median & ww!=.
drop ww_median 
 
 
 
/////////////financial constraints by firm size 
bys year: egen size_median=median(at)
gen fc_size=.
replace fc_size=1 if at<=size_median & at!=. 
replace fc_size=0 if at>size_median & at!=. 
drop size_median 



*************************First create Gala control variables********************

*physical capital k_phy=ppegt
gen lnK=ln(k_phy)
gen lnN_K=ln(emp/k_phy)
gen lnY_K=ln(sale/l.k_phy)


gen lnKtot=ln(k_tot)
gen lnN_Ktot=ln(emp/k_tot)
gen lnY_Ktot=ln(sale/l.k_tot)


////////////Higher order variables 

*square
gen TQ2=TQ^2
gen CF2=CF^2
gen lnK2=lnK^2
gen lnN_K2=lnN_K^2
gen lnY_K2=lnY_K^2
*cube
gen TQ3=TQ^3
gen CF3=CF^3
gen lnK3=lnK^3
gen lnN_K3=lnN_K^3

*leverage
gen lev=(dltt+dlc)/at

*net leverage
gen netlev=(dltt+dlc-che)/at


drop if missing(inv_phy)
drop if missing(inv_int)
drop if missing(inv_tot)
drop if missing(inv)
drop if missing(TQ)
drop if missing(q_tot)
drop if missing(CF)
drop if missing(CF_tot)
drop if missing(lnK)


winsor2  inv RD  CF  TQ lnK lnN_K, replace cuts(1 99) 
winsor2  lev netlev lnY_K, cut(1 99) replace
winsor2  inv_tot inv_phy inv_int q_tot CF_tot , cut(1 99) replace
winsor2  lnKtot lnN_Ktot lnY_Ktot , cut(1 99) replace
winsor2  TQ2 q_tot2 TQ3 q_tot3 lnY_K2, replace cut(1 99)
winsor2  tfp , cut(1 99) replace 

save  "${map}/fy1975", replace 




****************************Table 1 Summary Statistics**************************
use "${map}/fy1975"
tabstat inv inv_tot inv_phy inv_int TQ CF q_tot CF_tot lnK lnN_K lev netlev lnY_K tfp,statistics(co mean sd p25 p50 p75) columns(statistics), if lnN_K!=. &lnK!=.


xtset gvkey year
***********************Table 2 GMM with TFP *******************

* Column (1) with TFP only and TFP(t) is exogenous
* d.l3.TQ d.l4.TQ
eststo clear
eststo: gmm(d.inv-{b2}*d.l.TQ-{b3}*d.CF-{b4}*d.l.lnK-{b5}*d.l.lnN_K-{b6}*d.tfp),/*
*/instruments(d.CF d.l.lnK d.l.lnN_K d.tfp d.l3.TQ d.l4.TQ, nocons)/*
*/wmatrix(cluster gvkey) vce(cluster gvkey)
di e(Q)
esttab using ICC1.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv d.CF d.l.lnK d.l.lnN_K d.tfp (d.l.TQ =d.l3.TQ d.l4.TQ ), ///
first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey)
mat l e(first)





* Column (2) estimate the main results with productivity only but without TFP(t)  
* to match the TFP sample for consistent comparison
* l3.inv l4.inv l4.TQ  l5.CF

eststo clear
eststo: gmm(d.inv-{b1}*l.d.inv-{b2}*d.l.TQ+{b2}*{b1}*d.l2.TQ-{b3}*d.CF+{b3}*{b1}*d.l.CF-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnN_K+{b5}*{b1}*d.l2.lnN_K), instruments(d.CF d.l.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K l3.inv l4.inv l4.TQ  l5.CF, nocons) wmatrix(cluster gvkey) vce(cluster gvkey), if tfp!=.
di e(Q)
esttab using ICC1.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv d.l.CF d.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K (l.d.inv d.l.TQ l2.d.TQ=l3.inv l4.inv l4.TQ  l5.CF), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey), if tfp!=.
mat l e(first)





* Column (3) GMM with both productivity and TFP(t)
* with productivity and TFP (t) that is exogenous like CF, lnK, and lnN_K
* l4.CF l4.inv l4.TQ l5.TQ

eststo clear
eststo: gmm(d.inv-{b1}*l.d.inv-{b2}*d.l.TQ+{b2}*{b1}*d.l2.TQ-{b3}*d.CF+{b3}*{b1}*d.l.CF/*
*/-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnN_K+{b5}*{b1}*d.l2.lnN_K-{b6}*d.tfp+{b6}*{b1}*d.l.tfp),/*
*/instruments(d.CF d.l.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K d.tfp d.l.tfp l4.CF l4.inv l4.TQ l5.TQ, nocons) wmatrix(cluster gvkey) vce(cluster gvkey)
di e(Q)
esttab using ICC1.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv d.l.CF d.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K d.tfp d.l.tfp (d.l.inv d.l.TQ l2.d.TQ=l4.CF l4.inv l4.TQ l5.TQ),first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey)
mat l e(first)





*********************Table 3 Main results full sample 1975-2021*****************

* Column (1) 
reg d.inv  l.d.TQ d.CF d.l.lnK d.l.lnN_K , nocons  vce(cluster gvkey)


* Column (2) with MA(1) measurement error and without omega
* d.l3.TQ d.l4.TQ
eststo clear
eststo: gmm(d.inv-{b2}*d.l.TQ-{b3}*d.CF-{b4}*d.l.lnK-{b5}*d.l.lnN_K),/*
*/instruments(d.CF d.l.lnK d.l.lnN_K d.l3.TQ d.l4.TQ, nocons)/*
*/wmatrix(cluster gvkey) vce(cluster gvkey)
di e(Q)
esttab using ICC2.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv d.CF d.l.lnK d.l.lnN_K  (d.l.TQ =d.l3.TQ d.l4.TQ ), ///
first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey)
mat l e(first)


* Column (3) 
* MA(1) meassurement error and omega
* l4.CF l4.TQ l5.TQ l3.inv 
eststo clear
eststo: gmm(d.inv-{b1}*l.d.inv-{b2}*d.l.TQ+{b2}*{b1}*d.l2.TQ-{b3}*d.CF+{b3}*{b1}*d.l.CF),/*
*/instruments(d.CF d.l.CF l4.CF l4.TQ l5.TQ l3.inv , nocons)/*
*/wmatrix(cluster gvkey) vce(cluster gvkey)
di e(Q)
esttab using ICC2.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv d.l.CF d.CF (l.d.inv d.l.TQ l2.d.TQ=l4.CF l4.TQ l5.TQ l3.inv ), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey)
mat l e(first)




* Column (4) 
* MA(1) meassurement error and omega
* l3.inv l4.inv l4.TQ  l5.CF
eststo clear
eststo: gmm(d.inv-{b1}*l.d.inv-{b2}*d.l.TQ+{b2}*{b1}*d.l2.TQ-{b3}*d.CF+{b3}*{b1}*d.l.CF-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnN_K+{b5}*{b1}*d.l2.lnN_K),/*
*/instruments(d.CF d.l.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K l3.inv l4.inv l4.TQ l5.CF, nocons)/*
*/wmatrix(cluster gvkey) vce(cluster gvkey)
di e(Q)
esttab using ICC2.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv d.l.CF d.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K (l.d.inv d.l.TQ l2.d.TQ=l3.inv l4.inv l4.TQ  l5.CF ), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey)
mat l e(first)




* Column (5) 
* with omega but without measurement error
* l3.inv l3.TQ l3.CF l4.inv
eststo clear
eststo: gmm(d.inv-{b1}*l.d.inv-{b2}*d.l.TQ+{b2}*{b1}*d.l2.TQ-{b3}*d.CF+{b3}*{b1}*d.l.CF-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnN_K+{b5}*{b1}*d.l2.lnN_K),/*
*/instruments(d.CF d.l.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K l3.inv l3.TQ l3.CF l4.inv, nocons)/*
*/wmatrix(cluster gvkey) vce(cluster gvkey)
di e(Q)
esttab using ICC2.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv d.l.CF d.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K (l.d.inv d.l.TQ l2.d.TQ=l3.inv l3.TQ l3.CF l4.inv), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey)
mat l e(first)



* Column (6) 
* with MA(2) measurement error and omega
* l3.inv l4.inv l5.TQ  l5.CF
eststo clear
eststo: gmm(d.inv-{b1}*l.d.inv-{b2}*d.l.TQ+{b2}*{b1}*d.l2.TQ-{b3}*d.CF+{b3}*{b1}*d.l.CF-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnN_K+{b5}*{b1}*d.l2.lnN_K),instruments(d.CF d.l.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K l3.inv l4.inv l5.TQ  l5.CF, nocons) wmatrix(cluster gvkey) vce(cluster gvkey)
di e(Q)
esttab using ICC2.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv d.l.CF d.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K (l.d.inv d.l.TQ l2.d.TQ=l3.inv l4.inv l5.TQ  l5.CF), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey)
mat l e(first)




***********************************Table 4 with leverage************************

/***********Panel A GMM**************/
* Column (1) Leverage without sales(lnY_K)
* l4.lnK l3.inv l4.TQ l5.lnN_K 
eststo clear
eststo: gmm(d.inv-{b1}*l.d.inv-{b2}*d.l.TQ+{b2}*{b1}*d.l2.TQ-{b3}*d.l.lev+{b3}*{b1}*d.l2.lev-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnN_K+{b5}*{b1}*d.l2.lnN_K),/*
*/instruments(d.l.lev d.l2.lev d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K l4.lnK l3.inv l4.TQ l5.lnN_K , nocons) wmatrix(cluster gvkey) vce(cluster gvkey)
di e(Q)
esttab using ICC3.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv d.l.lev d.l2.lev d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K (l.d.inv d.l.TQ l2.d.TQ=l4.lnK l3.inv l4.TQ l5.lnN_K ), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey)
mat l e(first)

* Column (2) Net Leverage without sales(lnY_K)
* d.l4.netlev l3.inv l4.TQ l5.lnN_K
eststo clear
eststo: gmm(d.inv-{b1}*l.d.inv-{b2}*d.l.TQ+{b2}*{b1}*d.l2.TQ-{b3}*d.l.netlev+{b3}*{b1}*d.l2.netlev-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnN_K+{b5}*{b1}*d.l2.lnN_K), instruments(d.l.netlev d.l2.netlev d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K d.l4.netlev l3.inv l4.TQ l5.lnN_K, nocons) wmatrix(cluster gvkey) vce(cluster gvkey)
di e(Q)
esttab using ICC3.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv d.l.netlev d.l2.netlev d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K (l.d.inv d.l.TQ l2.d.TQ=d.l4.netlev l3.inv l4.TQ l5.lnN_K ), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey)
mat l e(first)


* Column (3) Leverage with sales(lnY_K)
* l4.lnK l4.d.TQ l5.lnY_K l3.inv 
eststo clear
eststo: gmm(d.inv-{b1}*l.d.inv-{b2}*d.l.TQ+{b2}*{b1}*d.l2.TQ-{b3}*d.l.lev+{b3}*{b1}*d.l2.lev-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnN_K+{b5}*{b1}*d.l2.lnN_K-{b6}*d.l.lnY_K+{b6}*{b1}*d.l2.lnY_K), instruments(d.l.lev d.l2.lev d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K d.l.lnY_K d.l2.lnY_K l4.lnK l4.d.TQ l5.lnY_K l3.inv , nocons) wmatrix(cluster gvkey) vce(cluster gvkey)
di e(Q)
esttab using ICC3.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv d.l.lev d.l2.lev d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K d.l.lnY_K d.l2.lnY_K (l.d.inv d.l.TQ l2.d.TQ=l4.lnK l4.d.TQ l5.lnY_K l3.inv ), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey)
mat l e(first)


* Column (4) Net leverage with sales(lnY_K)
* l4.lnK l4.d.TQ l5.lnY_K l3.inv 
eststo clear
eststo: gmm(d.inv-{b1}*l.d.inv-{b2}*d.l.TQ+{b2}*{b1}*d.l2.TQ-{b3}*d.l.netlev+{b3}*{b1}*d.l2.netlev-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnN_K+{b5}*{b1}*d.l2.lnN_K-{b6}*d.l.lnY_K+{b6}*{b1}*d.l2.lnY_K), instruments(d.l.netlev d.l2.netlev d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K d.l.lnY_K d.l2.lnY_K l4.lnK l4.d.TQ l5.lnY_K l3.inv , nocons) wmatrix(cluster gvkey) vce(cluster gvkey)
di e(Q)
esttab using ICC3.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv d.l.netlev d.l2.netlev d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K d.l.lnY_K d.l2.lnY_K (l.d.inv d.l.TQ l2.d.TQ=l4.lnK l4.d.TQ l5.lnY_K l3.inv ), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey)
mat l e(first)


/**************Panel B OLS**************/

reg d.inv l.d.TQ d.l.lev d.l.lnK d.l.lnN_K , nocons   vce(cluster gvkey)

reg d.inv l.d.TQ d.l.netlev d.l.lnK d.l.lnN_K , nocons  vce(cluster gvkey)

reg d.inv l.d.TQ d.l.lev d.l.lnK d.l.lnN_K d.l.lnY_K, nocons  vce(cluster gvkey)

reg d.inv l.d.TQ d.l.netlev d.l.lnK d.l.lnN_K  d.l.lnY_K, nocons  vce(cluster gvkey)




****************************Table 5 subperiods**********************************

* OLS 
* Column (1) year<=2010
reg d.inv  l.d.TQ d.CF d.l.lnK d.l.lnN_K , nocons  vce(cluster gvkey), if year<2010

* Column (2) year>=2010
reg d.inv  l.d.TQ d.CF d.l.lnK d.l.lnN_K , nocons  vce(cluster gvkey), if year>=2010


* GMM 
* Column (3) year<=2010
*l4.CF l5.CF l4.TQ l3.inv
eststo clear
eststo: gmm(d.inv-{b1}*l.d.inv-{b2}*d.l.TQ+{b2}*{b1}*d.l2.TQ-{b3}*d.CF+{b3}*{b1}*d.l.CF-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnN_K+{b5}*{b1}*d.l2.lnN_K), instruments(d.CF d.l.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K l4.CF l5.CF l4.TQ l3.inv, nocons) wmatrix(cluster gvkey) vce(cluster gvkey),  if year<2010
di e(Q)
esttab using ICC4.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv d.l.CF d.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K (l.d.inv d.l.TQ l2.d.TQ=l4.CF l5.CF l4.TQ l3.inv), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey),  if year<2010
mat l e(first)


* Column (4) year>=2010
*d.l4.CF d.l5.CF l4.TQ l3.inv
eststo clear
eststo: gmm(d.inv-{b1}*l.d.inv-{b2}*d.l.TQ+{b2}*{b1}*d.l2.TQ-{b3}*d.CF+{b3}*{b1}*d.l.CF-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnN_K+{b5}*{b1}*d.l2.lnN_K), instruments(d.CF d.l.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K d.l4.CF d.l5.CF l4.TQ l3.inv, nocons) wmatrix(cluster gvkey) vce(cluster gvkey), if year>=2010
di e(Q)
esttab using ICC4.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv d.l.CF d.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K (l.d.inv d.l.TQ l2.d.TQ=d.l4.CF d.l5.CF l4.TQ l3.inv), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey), if year>=2010
mat l e(first)





****************************Table 6 other investments***************************

/////////////////Panel A GMM 
* Column (1) total investment
* l4.inv_tot l3.inv_tot l5.CF_tot l4.q_tot 

eststo clear
eststo: gmm(d.inv_tot-{b1}*l.d.inv_tot-{b2}*d.l.q_tot+{b2}*{b1}*d.l2.q_tot-{b3}*d.CF_tot+{b3}*{b1}*d.l.CF_tot-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnN_K+{b5}*{b1}*d.l2.lnN_K), instruments(d.CF_tot d.l.CF_tot d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K l4.inv_tot l3.inv_tot l5.CF_tot l4.q_tot, nocons) wmatrix(cluster gvkey) vce(cluster gvkey)
di e(Q)
esttab using ICC5.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv_tot d.l.CF_tot d.CF_tot d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K (l.d.inv_tot d.l.q_tot l2.d.q_tot=l4.inv_tot l3.inv_tot l5.CF_tot l4.q_tot  ), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey)
mat l e(first)



* Column (2) physical investment
* l4.lnK l4.q_tot l5.q_tot l3.inv_phy

eststo clear
eststo: gmm(d.inv_phy-{b1}*l.d.inv_phy-{b2}*d.l.q_tot+{b2}*{b1}*d.l2.q_tot-{b3}*d.CF_tot+{b3}*{b1}*d.l.CF_tot-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnN_K+{b5}*{b1}*d.l2.lnN_K), instruments(d.CF_tot d.l.CF_tot d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K l4.lnK l4.q_tot l5.q_tot l3.inv_phy, nocons) wmatrix(cluster gvkey) vce(cluster gvkey)
di e(Q)
esttab using ICC5.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv_phy d.l.CF_tot d.CF_tot d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K (l.d.inv_phy d.l.q_tot l2.d.q_tot=l4.lnK l4.q_tot l5.q_tot l3.inv_phy ), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey)
mat l e(first)


* Column (3) intangible investment
* d.l4.lnN_K l4.q_tot l5.q_tot l3.inv_int

eststo clear
eststo: gmm(d.inv_int-{b1}*l.d.inv_int-{b2}*d.l.q_tot+{b2}*{b1}*d.l2.q_tot-{b3}*d.CF_tot+{b3}*{b1}*d.l.CF_tot-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnN_K+{b5}*{b1}*d.l2.lnN_K), instruments(d.CF_tot d.l.CF_tot d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K d.l4.lnN_K l4.q_tot l5.q_tot l3.inv_int, nocons) wmatrix(cluster gvkey) vce(cluster gvkey)
di e(Q)
esttab using ICC5.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv_int d.l.CF_tot d.CF_tot d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K (l.d.inv_int d.l.q_tot l2.d.q_tot=d.l4.lnN_K l4.q_tot l5.q_tot l3.inv_int  ), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey)
mat l e(first)



///////////////Panel B OLS with Gala et al state variables

reg d.inv_tot l.d.q_tot d.CF_tot d.l.lnK d.l.lnN_K , nocons  vce(cluster gvkey)

reg d.inv_phy l.d.q_tot d.CF_tot d.l.lnK d.l.lnN_K , nocons  vce(cluster gvkey)

reg d.inv_int l.d.q_tot d.CF_tot d.l.lnK d.l.lnN_K , nocons  vce(cluster gvkey)




*****************************Table 7 Non-linear********************************* 

* Column (1) 
* l5.lnN_K d.l4.lnK l4.TQ2 l5.TQ2 l4.TQ l3.inv 
eststo clear
eststo: gmm(d.inv-{b1}*l.d.inv-{b2}*d.l.TQ+{b2}*{b1}*d.l2.TQ-{b3}*d.CF+{b3}*{b1}*d.l.CF-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnK2+{b5}*{b1}*d.l2.lnK2-{b6}*d.l.lnN_K+{b6}*{b1}*d.l2.lnN_K-{b7}*d.l.lnN_K2+{b7}*{b1}*d.l2.lnN_K2), instruments(d.CF d.l.CF d.l.lnK d.l2.lnK d.l.lnK2 d.l2.lnK2 d.l.lnN_K d.l2.lnN_K d.l.lnN_K2 d.l2.lnN_K2 l5.lnN_K d.l4.lnK l4.TQ2 l5.TQ2 l4.TQ l3.inv , nocons) wmatrix(cluster gvkey) vce(cluster gvkey)
di e(Q)
esttab using ICC6.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv d.l.CF d.CF d.l.lnK d.l2.lnK d.l.lnK2 d.l2.lnK2 d.l.lnN_K d.l2.lnN_K d.l.lnN_K2 d.l2.lnN_K2 (l.d.inv d.l.TQ l2.d.TQ =l5.lnN_K d.l4.lnK l4.TQ2 l5.TQ2 l4.TQ l3.inv ), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey)
mat l e(first)


* Column (2) Non-linear model lnK^2 lnK^3 
* l5.lnN_K d.l4.lnK l4.TQ2 l5.TQ2 l4.TQ l3.inv 

eststo clear
eststo: gmm(d.inv-{b1}*l.d.inv-{b2}*d.l.TQ+{b2}*{b1}*d.l2.TQ-{b3}*d.CF+{b3}*{b1}*d.l.CF/*
*/-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnK2+{b5}*{b1}*d.l2.lnK2-{b6}*d.l.lnK3+{b6}*{b1}*d.l2.lnK3/*
*/-{b7}*d.l.lnN_K+{b7}*{b1}*d.l2.lnN_K), instruments(d.CF d.l.CF d.l.lnK d.l2.lnK d.l.lnK2 d.l2.lnK2 d.l.lnK3 d.l2.lnK3 d.l.lnN_K d.l2.lnN_K d.l.lnN_K2 d.l2.lnN_K2 d.l.lnN_K3 d.l2.lnN_K3 l5.lnN_K d.l4.lnK l4.TQ2 l5.TQ2 l4.TQ l3.inv , nocons) wmatrix(cluster gvkey) vce(cluster gvkey)
di e(Q)
esttab using ICC6.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv d.l.CF d.CF d.l.lnK d.l2.lnK d.l.lnK2 d.l2.lnK2 d.l.lnK3 d.l2.lnK3 d.l.lnN_K d.l2.lnN_K (l.d.inv d.l.TQ l2.d.TQ = l5.lnN_K d.l4.lnK l4.TQ2 l5.TQ2 l4.TQ l3.inv ), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey)
mat l e(first)






/******************************** Appendix*******************************/


/////////////////////////Appendix A Table A1 higher order OLS
* Column (1) OLS with lnK lnK2 lnN_K lnN_K2

reg d.inv d.CF d.CF2 d.l.lnK d.l.lnK2 d.l.lnN_K d.l.lnN_K2 , nocons  vce(cluster gvkey), if d.tfp !=.

*Column (2) OLS with TQ2 TQ3 CF2 state variables lnK lnK2 lnK3 lnN_K lnN_K2 lnN_K3
reg d.inv d.l.TQ d.l.TQ2 d.l.TQ3 d.CF d.CF2 d.l.lnK d.l.lnK2 d.l.lnK3 d.l.lnN_K d.l.lnN_K2 d.l.lnN_K3, nocons  vce(cluster gvkey), if d.tfp !=.


*Column (3) OLS with state variables and TFP
reg d.inv d.tfp  l.d.TQ d.CF d.l.lnK d.l.lnN_K , nocons   vce(cluster gvkey)

*Column (4) OLS higher order with TFP 
reg d.inv d.tfp d.l.TQ d.l.TQ2 d.l.TQ3 d.CF d.CF2 d.l.lnK d.l.lnK2 d.l.lnK3 d.l.lnN_K d.l.lnN_K2 d.l.lnN_K3 , nocons  vce(cluster gvkey)

*Column (5) OLS higher order with TFP, firm FE and year FE
xtreg d.inv d.tfp d.l.TQ d.l.TQ2 d.l.TQ3 d.CF d.CF2 d.l.lnK d.l.lnK2 d.l.lnK3 d.l.lnN_K d.l.lnN_K2 d.l.lnN_K3   i.year, fe    vce(cluster gvkey)




/////////////////////////Appendix B Financially constraints
xtset gvkey year

* financially constraint by size
* d.l3.inv d.l4.inv d.l4.TQ l4.lnK
forvalues i=0(1)1{
eststo clear
eststo: gmm(d.inv-{b1}*l.d.inv-{b2}*d.l.TQ+{b2}*{b1}*d.l2.TQ-{b3}*d.CF+{b3}*{b1}*d.l.CF-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnN_K+{b5}*{b1}*d.l2.lnN_K),/*
*/instruments(d.CF d.l.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K d.l3.inv d.l4.inv l4.TQ l4.lnK, nocons)wmatrix(cluster gvkey) vce(cluster gvkey), if fc_size==`i'
di e(Q)

*esttab using ICC2.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

* First stage
ivreg2 d.inv d.l.CF d.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K (l.d.inv d.l.TQ l2.d.TQ=d.l3.inv d.l4.inv l4.TQ l4.lnK), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey), if fc_size==`i'
mat l e(first)	
}




* financially constraint by WW index 
forvalues i=0(1)1{
eststo clear
eststo: gmm(d.inv-{b1}*l.d.inv-{b2}*d.l.TQ+{b2}*{b1}*d.l2.TQ-{b3}*d.CF+{b3}*{b1}*d.l.CF-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnN_K+{b5}*{b1}*d.l2.lnN_K),/*
*/instruments(d.CF d.l.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K d.l3.inv d.l4.inv l4.TQ l4.lnK, nocons)wmatrix(cluster gvkey) vce(cluster gvkey), if fc_ww==`i'
di e(Q)
*esttab using ICC2.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage

ivreg2 d.inv d.l.CF d.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K (l.d.inv d.l.TQ l2.d.TQ=d.l3.inv d.l4.inv l4.TQ l4.lnK), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey), if fc_ww==`i'
mat l e(first)
}



* financially constraint by KZ index 
forvalues i=0(1)1{
eststo clear
eststo: gmm(d.inv-{b1}*l.d.inv-{b2}*d.l.TQ+{b2}*{b1}*d.l2.TQ-{b3}*d.CF+{b3}*{b1}*d.l.CF-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnN_K+{b5}*{b1}*d.l2.lnN_K),/*
*/instruments(d.CF d.l.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K d.l3.inv d.l4.inv l4.TQ l4.lnK, nocons)wmatrix(cluster gvkey) vce(cluster gvkey), if fc_kz==`i'
di e(Q)

*esttab using ICC2.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv d.l.CF d.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K (l.d.inv d.l.TQ l2.d.TQ=d.l3.inv d.l4.inv l4.TQ l4.lnK), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey), if fc_kz==`i'
mat l e(first)
}






///////////////////////Appendix C. Table C1 
* omega is measured at t-1 and using lagged TFP as IV

* Column (1) Original Results in Table 3 Column (4) for comparison

* Column (2)
* when omega ω at t-1 
* d.l4.CF l4.TQ d.l4.inv d.l5.CF

eststo clear
eststo: gmm(d.inv-{b1}*l.d.inv-{b2}*d.l.TQ+{b2}*{b1}*d.l2.TQ-{b3}*d.CF+{b3}*{b1}*d.l.CF-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnN_K+{b5}*{b1}*d.l2.lnN_K), instruments(d.CF d.l.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K d.l4.CF l4.TQ d.l4.inv d.l5.CF , nocons) wmatrix(cluster gvkey) vce(cluster gvkey)
di e(Q)
esttab using ICC7.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv d.l.CF d.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K (l.d.inv d.l.TQ l2.d.TQ=d.l4.CF l4.TQ d.l4.inv d.l5.CF ), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey)
mat l e(first)

* Column (3)
* using lagged TFP as IV 
* l3.tfp l4.CF l4.TQ l4.inv 

eststo clear
eststo: gmm(d.inv-{b1}*l.d.inv-{b2}*d.l.TQ+{b2}*{b1}*d.l2.TQ-{b3}*d.CF+{b3}*{b1}*d.l.CF-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnN_K+{b5}*{b1}*d.l2.lnN_K), instruments(d.CF d.l.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K l4.CF l4.TQ l4.inv l3.tfp, nocons) wmatrix(cluster gvkey) vce(cluster gvkey)
di e(Q)
esttab using ICC7.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv d.l.CF d.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K (l.d.inv d.l.TQ l2.d.TQ=l4.CF l4.TQ l4.inv l3.tfp ), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey)
mat l e(first)


///////////////////////Appendix C. Table C2 
*Instrument robustness: 3-4 lags 
 
xtset gvkey year
* Column (1) Original Results in Table 3 Column (4) for comparison

* Column (2)
* l3.inv l4.inv l4.TQ  d.l4.lnN_K
eststo clear
eststo: gmm(d.inv-{b1}*l.d.inv-{b2}*d.l.TQ+{b2}*{b1}*d.l2.TQ-{b3}*d.CF+{b3}*{b1}*d.l.CF-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnN_K+{b5}*{b1}*d.l2.lnN_K),/*
*/instruments(d.CF d.l.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K l3.inv l4.inv l4.TQ  d.l4.lnN_K, nocons) wmatrix(cluster gvkey) vce(cluster gvkey)
di e(Q)
esttab using ICC8.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv d.l.CF d.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K (l.d.inv d.l.TQ l2.d.TQ=l3.inv l4.inv l4.TQ  d.l4.lnN_K), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey)
mat l e(first)

* Column (3) 
* l3.inv l4.inv d.l4.CF  l4.TQ
eststo clear
eststo: gmm(d.inv-{b1}*l.d.inv-{b2}*d.l.TQ+{b2}*{b1}*d.l2.TQ-{b3}*d.CF+{b3}*{b1}*d.l.CF-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnN_K+{b5}*{b1}*d.l2.lnN_K),/*
*/instruments(d.CF d.l.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K l3.inv l4.inv d.l4.CF l4.TQ, nocons) wmatrix(cluster gvkey) vce(cluster gvkey)
di e(Q)
esttab using ICC8.csv, replace  b(%5.3f) starlevels( * 0.10 ** 0.05 *** 0.010)

*First stage
ivreg2 d.inv d.l.CF d.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K (l.d.inv d.l.TQ l2.d.TQ=l3.inv l4.inv d.l4.CF l4.TQ), first savefirst nocons savefprefix(st4) gmm2s robust cluster(gvkey)
mat l e(first)



//////////////////////Appendix D. AR Tests


************Column (1) TFP 1975-2021 AR(2)**********

use "${map}/fy1975"
xtset gvkey year

collapse (mean) tfp, by(year)

tsset year
arima tfp, ar(1/2) 

clear


* Estimation of Table 3 Column (4) MA(1) meassurement error 
* GMM CF with Gala et al's state variables l3.inv l4.inv l4.TQ  l5.CF
use "${map}/fy1975"
eststo clear
eststo: gmm(eq: d.inv-{b1}*l.d.inv-{b2}*d.l.TQ+{b2}*{b1}*d.l2.TQ-{b3}*d.CF+{b3}*{b1}*d.l.CF-{b4}*d.l.lnK+{b4}*{b1}*d.l2.lnK-{b5}*d.l.lnN_K+{b5}*{b1}*d.l2.lnN_K),/*
*/instruments(eq: d.CF d.l.CF d.l.lnK d.l2.lnK d.l.lnN_K d.l2.lnN_K l3.inv l4.inv l5.CF l4.TQ, nocons )/*
*/wmatrix(cluster gvkey) vce(cluster gvkey)
di e(Q)

*AR (2) P-value
xtset gvkey year
predict uhat_new, resid
collapse (mean) uhat, by(year)

tsset year
******Column (2) 
arima uhat, ar(1/2) 
******Column (3) 
arima uhat, ar(1) 

clear



















